package com.hotel.config.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayConfig {
    /**
     * 秒杀业务
     */
    // 秒杀交换机
    public static final String SECKIL_COUPON_EXCHANGE = "seckil.coupon.exchange";
    // 秒杀队列
    public static final String SECKIL_COUPON_QUEUE = "seckil.coupon.queue";
    // 路由key
    public static final String SECKIL_COUPON_ROUTER_KEY = "seckli.coupon.router.key";

    /**
     * 死信交换机
     */
    // 秒杀交换机
    public static final String DEAD_COUPON_EXCHANGE = "dead.coupon.exchange";
    // 秒杀队列
    public static final String DEAD_COUPON_QUEUE = "dead.coupon.queue";
    // 路由key
    public static final String DEAD_COUPON_ROUTER_KEY = "dead.coupon.router.key";

    /**
     * 优惠券秒杀个人中心业务
     */
    // 延时交换机
    public static final String DELAYED_COUPON_EXCHANGE = "delayed.coupon.exchange";
    // 延时队列
    public static final String DELAYED_COUPON_QUEUE = "delayed.coupon.queue";
    // 延时路由key
    public static final String DELAYED_COUPON_ROUTER_KEY = "delayed.coupon.router.key";

    /*秒杀业务*/

    // 秒杀交换机
    @Bean("seckilAchunExchange")
    public DirectExchange seckilExchange() {
       return new DirectExchange(SECKIL_COUPON_EXCHANGE,true,false);
    }

    // 秒杀队列
    @Bean("seckilAchunQueue")
    public Queue seckilQueue() {
        Map<String,Object> map = new HashMap<>();
        map.put("x-message-ttl",5*1000); // 过期时间5s
        map.put("x-max-length",10); // 队列最大长度
        map.put("x-dead-letter-exchange",DEAD_COUPON_EXCHANGE); // 死信交换机
        map.put("x-dead-letter-routing-key",DEAD_COUPON_ROUTER_KEY); // 死信路由
        return new Queue(SECKIL_COUPON_QUEUE,true,false,false);
    }

    // 秒杀绑定
    @Bean("seckilAchunBind")
    public Binding bindingSeckilQueue(@Qualifier("seckilAchunQueue") Queue queue,
                                      @Qualifier("seckilAchunExchange") DirectExchange
                                              directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with(SECKIL_COUPON_ROUTER_KEY);
    }

    /*死信*/

    // 死信交换机
    @Bean("deadAchunExchange")
    public DirectExchange deadExchange() {
        return new DirectExchange(DEAD_COUPON_EXCHANGE,true,false);
    }

    // 死信队列
    @Bean("deadAchunQueue")
    public Queue deadQueue() {
        return new Queue(DEAD_COUPON_QUEUE,true,false,false);
    }

    // 死信绑定
    @Bean("deadAchunBing")
    public Binding bindingDeadQueue(@Qualifier("deadAchunQueue") Queue queue,
                                    @Qualifier("deadAchunExchange") DirectExchange
                                            directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with(DEAD_COUPON_ROUTER_KEY);
    }

    /*延时业务*/

    // 延时交换机
    @Bean("delayedAchunExchange")
    public CustomExchange customExchange() {
        Map<String,Object> map = new HashMap<>();
        map.put("x-delayed-type","direct"); // 延迟交换机类型
        return new CustomExchange(DELAYED_COUPON_EXCHANGE,"x-delayed-message",true,false,map);
    }

    // 延时队列
    @Bean("delayedAchunQueue")
    public Queue queue() {
        return new Queue(DELAYED_COUPON_QUEUE,true,false,false);
    }

    // 延时绑定
    @Bean("delayAchunBing")
    public Binding bindingDelayedQueue(@Qualifier("delayedAchunQueue") Queue queue,
                                       @Qualifier("delayedAchunExchange") CustomExchange
                                               delayedExchange) {
        return BindingBuilder.bind(queue).to(delayedExchange).with(DELAYED_COUPON_ROUTER_KEY).noargs();
    }
}
